<?php
namespace app\admin\controller;

use app\admin\controller\Base;
use think\Db;
class Maintain extends Base
{

    /**
     * 数据库操作
     */
    public function DataOperation()
    {
        //获取操作内容：（备份/下载/还原/删除）数据库
        $type = Param('type');
        //获取需要操作的数据库名字
        $name = Param('name');

        $backup = new Baksql(\think\Config::get("database"));

        switch ($type) {
            //备份
            case "backup":
                $info = $backup->backup();
                return ['status'=>1,'message'=>$info];
                break;
            //下载
            case "dowonload":
                $info = $backup->downloadFile($name);
                $this->success("$info", 'Maintain/DataOperation');
                break;
            //还原
            case "restore":
                $info = $backup->restore($name);
                $this->success("$info", 'Maintain/DataOperation');
                break;
            //删除
            case "del":
                $info = $backup->delfilename($name);
                $this->success("$info", 'Maintain/DataOperation');
                break;
            //如果没有操作，则查询已备份的所有数据库信息
            default:
                return $this->fetch("DataList", ["list" => array_reverse($backup->get_filelist())]);//将信息由新到老排序
        }
    }

    /**
     * 数据清空
     * @return array
     */
    public function dataDelete(){
        Db::name('user')->where('id > 1')->delete();    //会员表
        Db::name('user_address')->delete(true);    //会员收货地址
        Db::name('user_bank')->where('id > 0')->delete();    //银行表
        Db::name('user_code')->delete(true);  //短信验证码表
        Db::name('user_contact')->where('id > 1')->delete();  //接点表
        Db::name('user_level_apply')->where('id > 0')->delete();    //等级申请表
        Db::name('user_order')->where('id > 0')->delete();    //订单表
        Db::name('user_qrcode')->delete(true);  //二维码表
        Db::name('user_recharge')->delete(true); //充值表
        Db::name('user_recommend')->where('id > 1')->delete();  //推荐表
        Db::name('user_withdraw')->delete(true);  //提现表


        Db::name('feedback')->delete(true);  //反馈表
        Db::name('finance')->delete(true);  //奖金表

        Db::name('user')->where(['id' =>1])->update(['recommend_count' =>0,'team_num'=>0,'total_achievement'=>0]);
        Db::name('system_config')->where(['id' =>1])->update(['month_all' =>0,'money_all'=>0]);
        return ['status'=>1,'message'=>'清空数据成功'];
    }

    /**
     * 模拟日结算
     * @return array
     */
    public function SettlementDay(){
        Db::query('call execute_day()');
        return ['status'=>1,'message'=>'结算成功'];
    }

    /**
     * 模拟月结算
     * @return array
     */
    // public function SettlementMonth(){
    //     Db::query('call execute_month()');
    //     return ['status'=>1,'message'=>'结算成功'];
    // }
    public function SettlementMonth(){
        //系统参数
        $Config = Config();
        //三星董事
        $three = Db::name('user')->where(['level'=>12])->select();
        //人数
        $threecount = count($three);
        if($threecount != 0 && $Config['month_all'] != 0){
            //每人分得多少
            $money = $Config['month_all']*$Config['san']/$threecount;
            foreach ($three as $value){
                $tj_money = $money*$Config['yu'];//推荐奖余额
                $tj_money1 = $money*$Config['ji'];//推荐奖积分
                $new['balance_4']=$value['balance_4']+$tj_money;  //推荐奖励余额
                $new['balance_1']=$value['balance_1']+$tj_money1;  //推荐奖励积分
                $new['person_achievement']=$value['person_achievement']+$money;  //个人收益
                FinanceAdd($value['id'], $value['id'], $tj_money, 4, 11, '分红余额');
                FinanceAdd($value['id'], $value['id'], $tj_money1, 1, 11, '分红积分');
                Db::name('user')->where(['id'=>$value['id']])->update($new);
            }
        }
        //二星董事
        $two = Db::name('user')->where(['level'=>['gt',10]])->select();
        //人数
        $twocount = count($two);
        if($twocount != 0 && $Config['month_all'] != 0){
            //每人分得多少
            $money = $Config['month_all']*$Config['er']/$twocount;
            foreach ($two as $val){
                $tj_money = $money*$Config['yu'];//推荐奖余额
                $tj_money1 = $money*$Config['ji'];//推荐奖积分
                $new['balance_4']=$val['balance_4']+$tj_money;  //推荐奖励余额
                $new['balance_1']=$val['balance_1']+$tj_money1;  //推荐奖励积分
                $new['person_achievement']=$val['person_achievement']+$money;  //个人收益
                FinanceAdd($val['id'], $val['id'], $tj_money, 4, 11, '分红余额');
                FinanceAdd($val['id'], $val['id'], $tj_money1, 1, 11, '分红积分');
                Db::name('user')->where(['id'=>$val['id']])->update($new);
            }
        }
        //一星董事
        $one = Db::name('user')->where(['level'=>['gt',9]])->select();
        //人数
        $onecount = count($one);
        if($onecount != 0 && $Config['month_all'] != 0){
            //每人分得多少
            $money = $Config['month_all']*$Config['yi']/$onecount;
            foreach ($one as $v){
                $tj_money = $money*$Config['yu'];//余额
                $tj_money1 = $money*$Config['ji'];//积分
                $new['balance_4']=$v['balance_4']+$tj_money;  //余额
                $new['balance_1']=$v['balance_1']+$tj_money1;  //积分
                $new['person_achievement']=$v['person_achievement']+$money;  //个人收益
                FinanceAdd($v['id'], $v['id'], $tj_money, 4, 11, '分红余额');
                FinanceAdd($v['id'], $v['id'], $tj_money1, 1, 11, '分红积分');
                Db::name('user')->where(['id'=>$v['id']])->update($new);
            }
        }
        Db::name('system_config')->where(['id'=>1])->update(['month_all'=>0]);
        return ['status'=>1,'message'=>'结算成功'];
    }
    
    /**
     * 模拟释放
     * @return array
     */
    public function shifang(){
        //系统参数
        $Config = Config();
        //每天释放倍数
        $sf = $Config['shifang'];
        //获取所有需要释放的人
        $peoples = Db::name('user')->where(['sfjifen'=>['gt',0]])->select();
        if($peoples){
            foreach($peoples as $v){
                //释放的积分
                $jifen = $v['sfjifen']*$sf;
                $data['balance_1'] = $v['balance_1'] + $jifen;
                $data['sfjifen'] = $v['sfjifen'] - $jifen;
                FinanceAdd( $v['id'], $v['id'], $jifen, 4, 11, '积分释放');
                $data['person_achievement']=$v['person_achievement']+$jifen;  //个人收益
                Db::name('user')->where(['id'=>$v['id']])->update($data);
            }
        }
        // var_dump($data);die;
        return ['status'=>1,'message'=>'释放成功'];
    }
    
    
    //登录记录
    public function LoginList(){
        //条件查询
        $Data = Param();
        if (empty($Data)){
            $Data = [
            ];

        }

        $this->assign('Data',$Data);
        $Where = [];
        $allinfo = [];
        //等级查询
        
        //总记录条数
        $Count = LoginCount($Where);
        $this->assign('Count', $Count);

        //会员信息
        $UserList = LoginList($Where);
        //处理登录记录    如果type1 查admin    2查user
        foreach ($UserList['UserList'] as $v){
            if($v['type'] == 1){
                $admininfo = Db::name('admin')->where(['id'=>$v['user_id']])->find();
                if($admininfo){
                    $v['name'] = $admininfo['name'];
                }else{
                    $v['name'] = '';
                }
            }else{
                $userinfo = Db::name('user')->where(['id'=>$v['user_id']])->find();
                if($userinfo){
                    $v['name'] = $userinfo['username'];
                }else{
                    $v['name'] = '';
                }
            } 
            $allinfo[] = $v;
        }
        $this->assign('UserList', $allinfo);

        //分页
        $this->assign('Page', $UserList['Page']);

        //级别信息
        $LevelList = LevelList();
        $this->assign('LevelList', $LevelList);

        return $this->fetch('LoginList');
    }
    
    //登录记录删除
    public function logindelete(){
        if(IsPost()){
            $id = Param()['id'];
            if(Db::name('user_login')->where(['id'=>$id])->delete()){
                return ['status'=>1,'message'=>'删除成功'];
            }else{
                return ['status'=>0,'message'=>'系统错误，请重试'];
            }
        }
        
    }
    
    
}